function [rcmask,hfmask,lpmask,bsmask,finalmask] = UNSW_ArtifactMask(rawecg,railV,fm,fs)
%%
% [rcmask,hfmask,lpmask,bsmask,finalmask] = UNSW_ArtifactMask(rawecg,railV,fm,fs)
%
% Determines the ECG artifact mask as described in [1]
%
% IN:   rawecg      -   1xN double. The ECG samples (mV). N is the number of samples.
%       railV       -   1x2 double. Bottom and top rail voltages (mV).
%       fm          -   1x1 double. The mains frequency (Hz)
%       fs          -   1x1 double. The sampling frequency (Hz)
%
% OUT:  rcmask      -   1xM1 double. M1 is the number of masked samples.
%                       Indices in rawecg of the rail contact mask - See RC Mask in [1].
%       hfmask      -   1xM2 double. M2 is the number of masked samples.
%                       Indices in rawecg of the high frequency mask - See HF Mask in [1].
%       lpmask      -   1xM3 double. M3 is the number of masked samples.
%                       Indices in rawecg of the low power mask - See LP Mask in [1].
%       bsmask      -   1xM4 double. M4 is the number of masked samples.
%                       Indices in rawecg of the baseline shift mask - See BS Mask in [1].
%       finalmask	-   1xM5 double. M5 is the number of masked samples.
%                       Indices in rawecg of the final mask - See Final Mask in [1].
%
% This function uses the files sortfilt1.m, myfiltfilt.m, medianfilter.mexw64 and
% minmaxfilter.mexw64 - Ensure that these files are on the MATLAB path.
%
% If the .mexw64 files are incompatible with you version of MATLAB, please
% rebuild the mex files from the corresponding .cpp and .h files provided.
%
% If you use this code or data, please cite as follows:
%
% [1] H. Khamis, R. Weiss, Y. Xie, C-W. Chang, N. H. Lovell, S. J. Redmond, 
% "QRS detection algorithm for telehealth electrocardiogram recordings," 
% IEEE Transaction in Biomedical Engineering, vol. 63(7), p. 1377-1388,
% 2016.
%
% Last Modified: 05/09/2016
%

rawecg=rawecg(:);
N=length(rawecg);

% Get clean ECG (0.7 - 33 Hz)
[bpdata,lpdata] = cleansignal_ecgmask(rawecg,fs);

% Find mask for Rail contact
% 1% of range from rail voltages
bottomRail = railV(1);
topRail = railV(2);
voltageRange = topRail - bottomRail;
thresholdRailPercentage = 0.05; 
railmask1 = find(rawecg> topRail -(thresholdRailPercentage*voltageRange) | rawecg<bottomRail +(thresholdRailPercentage*voltageRange) );railmask1=railmask1(:);
% Dilate by 1 second (Using a max filter 2 seconds long)
temp=zeros(N,1);
temp(railmask1)=1;
temp = round(sortfilt1(temp,round(1*(2*fs)),100));
rcmask=find(temp==1);

% Square and integrate Filtered ECG signal to get LF power
[b]=hamming(round(0.05*fs));b=b/sum(abs(b));[a]=1;
lfpower=real(myfiltfilt(b,a,bpdata.^2).^0.5); 
% Find mask for Low Signal Power
lowpowermask1 = find(abs(lfpower) < 0.02); % in mV
temp=zeros(N,1);
temp(lowpowermask1)=1;
temp = [0;temp;0]; 
% Remove sections less than 3 seconds;
start=[];
for i=2:length(temp)
    if temp(i)-temp(i-1)==1
        start = i;
    end
    if temp(i)-temp(i-1)==-1 && (i-1)-start < 3*fs
       temp(start:(i-1))=0;
    end        
end
lpmask = find(temp(2:end-1));
lpmask=lpmask(:);

% Get HF power (with 50 Hz removed)
% Remove mains frequency
r=0.9;theta=2*pi*fm/fs;
b=[1 -2*cos(theta) 1];
a=[1 -2*r*cos(theta) (r^2)];
nolinedata = myfiltfilt(b,a,rawecg);
% highpass filter at 40Hz
[b,a] = ellip(5,0.5,80,40/(fs/2),'high');
hfcleandata = myfiltfilt(b,a,nolinedata);
% smooth the power
[b]=hamming(round(0.05*fs));b=b/sum(abs(b));[a]=1;
b=b./sum(abs(b));
hfpower=real(myfiltfilt(b,a,hfcleandata.^2).^0.5); 
% Find HF mask
hfmask = find((hfpower)>10*mean(hfpower));hfmask=hfmask(:); % in mV

% Get intermediate mask (used to estimate Baseline shift threshold)
intermask = unique([lpmask;rcmask;hfmask]);
% Estimate mean pk-pk amp of dilated ECG signal
interdata = bpdata;
interdata(intermask)=0;
Wsort = round(2*fs);
upperenv = sortfilt1(interdata,Wsort,100); % Morph Open
upperenv = sortfilt1(upperenv,Wsort,0); % Morph Close
lowerenv = sortfilt1(interdata,Wsort,0); % Morph Open
lowerenv = sortfilt1(lowerenv,Wsort,100); % Morph Close
pkpk = upperenv - lowerenv;
usefulidx = setdiff([1:N],intermask);
if ~isempty(usefulidx)
    meanAmp = mean(pkpk(usefulidx));
else
    meanAmp = 1.0; % mV (default estimate of pk-pk amplitude)
end
interdata(intermask)=NaN;
% Find Baseline shifts
shiftrate = 1.0; % mV/s
[baseerr] = baselineshift(lpdata,shiftrate,fs);
bsmask=find( baseerr > 0.2*meanAmp ); % in mV
% Fill in for short sections (less than 5 seconds)
penultimatemask = unique([lpmask;hfmask;rcmask;bsmask]);
temp=zeros(N,1);
temp(penultimatemask)=1;
temp = [1;temp;1]; 
% Fill in sections less than 5 seconds;
start=[];
for i=2:length(temp)
    if temp(i)-temp(i-1)==-1
        start = i;
    end
    if temp(i)-temp(i-1)==1 && (i-1)-start < 5*fs
       temp(start:(i-1))=1;
    end        
end
finalmask = find(temp(2:end-1));
finalmask = finalmask(:);

rcmask = rcmask';
hfmask = hfmask';
lpmask = lpmask';
bsmask = bsmask';
finalmask = finalmask';

%%
% baselineshift
%
% Helper function - Calculates a feature which increases for regions possibly containing baseline shift
% 
% x - ECG signal (prefereably bandpass filtered between 0.7 - 33 Hz)
% fs - sampling freq
% rate - initial speed at which tracker can chase the baseline
%
function [err] = baselineshift(x,rate,fs)

x=x(:);
N=length(x);

% Use of low pass linear phase filter
[b,a] = butter(5,[1]/(fs/2));
baseline = myfiltfilt(b,a,x);

y1 = tracker(baseline,fs,rate);
y2 = tracker(baseline(end:-1:1),fs,rate);
y2= y2(end:-1:1);
e1=baseline-y1;
e2=baseline-y2;
err = abs(y1-y2);

%%
% tracker
%
% Helper function - Chase baseline. Starting rate is 'rate'/s. It accelerates towards signal
% at increasing speed by 'rate' every second if it has not caught the signal
%
% x - signal 
% fs - sampling freq
% rate - maximum # of levels change per second allowed
%
function y = tracker(x,fs,rate)

maxshift = rate/fs;

y=zeros(length(x),1);
y(1)=x(1);
for k=2:length(x)
   
    e=x(k)-y(k-1);
    if(abs(e)<maxshift)
        maxshift = rate/fs;
        y(k)=x(k);
    else
        maxshift = maxshift+rate/(fs^2);
        y(k)=y(k-1)+sign(e)*(maxshift);
    end

end
y=y(:);

%%
% cleansignal_ecgmask
%
% Helper function - Filter ECG between 0.7 & 33 Hz
%
function [bpdata,lpdata] = cleansignal_ecgmask(x,fs)

x = detrend(x);

if fs<600 & fs > 400
    
     % Use of low pass linear phase filter
     coeffHH33 = [0.0000000000000000e+000,...
            1.4941369846753368e-003,...
            -1.0455045927741559e-002,...
            3.3500295952731456e-002,... 
            -6.4893944345397081e-002,...
            8.3797720317789912e-002,...
            -7.4257693331593280e-002,...
            4.4278595302074189e-002,...
            -1.6310590966700469e-002,...
            2.8508227082347745e-003     ;...
            1.0000000000000000e+000,...
            -7.8088655155147695e+000,...
            2.7427915261681783e+001,...
            -5.6857459912997783e+001,...
            7.6641650909363534e+001,...
            -6.9652101236901046e+001,...
            4.2670594466753961e+001,...
            -1.6990423939511487e+001,...
            3.9896046397380731e+000,...
            -4.2091037599832609e-001];

     num=coeffHH33(1,:);
     den=coeffHH33(2,:);

     lpdata = myfiltfilt(num,den,x);

     numhpz=[-3.9221262304045220e-004,...
        -3.9248810588436559e-004,...
        -3.9278557879888544e-004,...
        -3.9310503058520639e-004,...
        -3.9344644921534048e-004,...
        -3.9380982183519360e-004,...
        -3.9419513476445281e-004,...
        -3.9460237349535755e-004,...
        -3.9503152269589547e-004,...
        -3.9548256620951260e-004,...
        -3.9595548705403428e-004,...
        -3.9645026742427284e-004,...
        -3.9696688869288481e-004,...
        -3.9750533140803603e-004,...
         -3.9806557529708554e-004,...
         -3.9864759927138948e-004,...
         -3.9925138141260049e-004,...
         -3.9987689899230482e-004,...
         -4.0052412845809971e-004,...
         -4.0119304544340392e-004,...
         -4.0188362476384267e-004,...
         -4.0259584041861963e-004,...
         -4.0332966559388861e-004,...
         -4.0408507266161576e-004,...
         -4.0486203318067509e-004,...
         -4.0566051789899297e-004,...
         -4.0648049675459143e-004,...
         -4.0732193887510872e-004,...
         -4.0818481258101248e-004,...
         -4.0906908538611221e-004,...
         -4.0997472399742052e-004,...
         -4.1090169431744780e-004,...
         -4.1184996144816321e-004,...
         -4.1281948968544544e-004,...
         -4.1381024253105267e-004,...
         -4.1482218267794918e-004,...
         -4.1585527203154391e-004,...
         -4.1690947169599328e-004,...
         -4.1798474198001978e-004,...
         -4.1908104240096660e-004,...
         -4.2019833168408839e-004,...
         -4.2133656776299853e-004,...
         -4.2249570778469885e-004,...
         -4.2367570810830960e-004,...
         -4.2487652430682477e-004,...
         -4.2609811117052588e-004,...
         -4.2734042270785072e-004,...
         -4.2860341214514641e-004,...
         -4.2988703193259199e-004,...
         -4.3119123374318305e-004,...
         -4.3251596847251775e-004,...
         -4.3386118624743134e-004,...
         -4.3522683641794362e-004,...
         -4.3661286757293587e-004,...
         -4.3801922752547393e-004,...
         -4.3944586332736934e-004,...
         -4.4089272126770964e-004,...
         -4.4235974687193173e-004,...
         -4.4384688490793135e-004,...
         -4.4535407938599834e-004,...
         -4.4688127356072138e-004,...
         -4.4842840993406050e-004,...
         -4.4999543025777105e-004,...
         -4.5158227553346796e-004,...
         -4.5318888601808148e-004,...
         -4.5481520122371251e-004,...
         -4.5646115991992769e-004,...
         -4.5812670013784516e-004,...
         -4.5981175917219740e-004,...
         -4.6151627357636396e-004,...
         -4.6324017918234861e-004,...
         -4.6498341108255393e-004,...
         -4.6674590364742866e-004,...
         -4.6852759051817098e-004,...
         -4.7032840461614517e-004,...
         -4.7214827814207375e-004,...
         -4.7398714257819528e-004,...
         -4.7584492869213947e-004,...
         -4.7772156654013855e-004,...
         -4.7961698546680862e-004,...
         -4.8153111411124833e-004,...
         -4.8346388040789562e-004,...
         -4.8541521158890807e-004,...
         -4.8738503418738839e-004,...
         -4.8937327404175953e-004,...
         -4.9137985629451328e-004,...
         -4.9340470539832549e-004,...
         -4.9544774512297915e-004,...
         -4.9750889854377218e-004,...
         -4.9958808806302608e-004,...
         -5.0168523539876708e-004,...
         -5.0380026159520795e-004,...
         -5.0593308702328395e-004,...
         -5.0808363138209005e-004,...
         -5.1025181370564180e-004,...
         -5.1243755236310385e-004,...
         -5.1464076506255879e-004,...
         -5.1686136885394107e-004,...
         -5.1909928013370125e-004,...
         -5.2135441464469783e-004,...
         -5.2362668748273417e-004,...
         -5.2591601309848939e-004,...
         -5.2822230529983583e-004,...
         -5.3054547725488253e-004,...
         -5.3288544150024650e-004,...
         -5.3524210993461600e-004,...
         -5.3761539383509666e-004,...
         -5.4000520384260965e-004,...
         -5.4241144998692860e-004,...
         -5.4483404167353410e-004,...
         -5.4727288769203014e-004,...
         -5.4972789622166173e-004,...
         -5.5219897483346010e-004,...
         -5.5468603049121760e-004,...
         -5.5718896956008079e-004,...
         -5.5970769780566069e-004,...
         -5.6224212039870751e-004,...
         -5.6479214191959494e-004,...
         -5.6735766636233412e-004,...
         -5.6993859713464975e-004,...
         -5.7253483706756303e-004,...
         -5.7514628841464233e-004,...
         -5.7777285285410705e-004,...
         -5.8041443149969190e-004,...
         -5.8307092489380606e-004,...
         -5.8574223302553766e-004,...
         -5.8842825531853454e-004,...
         -5.9112889064588756e-004,...
         -5.9384403733179895e-004,...
         -5.9657359315098512e-004,...
         -5.9931745533793601e-004,...
         -6.0207552058760111e-004,...
         -6.0484768506032589e-004,...
         -6.0763384438548298e-004,...
         -6.1043389366675189e-004,...
         -6.1324772748252705e-004,...
         -6.1607523989440230e-004,...
         -6.1891632444770021e-004,...
         -6.2177087417691402e-004,...
         -6.2463878161027884e-004,...
         -6.2751993877498320e-004,...
         -6.3041423719100310e-004,...
         -6.3332156789659526e-004,...
         -6.3624182142818747e-004,...
         -6.3917488784185901e-004,...
         -6.4212065670716915e-004,...
         -6.4507901711889008e-004,...
         -6.4804985769664845e-004,...
         -6.5103306658998599e-004,...
         -6.5402853148223731e-004,...
         -6.5703613959677658e-004,...
         -6.6005577769744604e-004,...
         -6.6308733209736776e-004,...
         -6.6613068866025276e-004,...
         -6.6918573280565197e-004,...
         -6.7225234951211036e-004,...
         -6.7533042332464576e-004,...
         -6.7841983835399385e-004,...
         -6.8152047828467740e-004,...
         -6.8463222638380050e-004,...
         -6.8775496549020585e-004,...
         -6.9088857803816222e-004,...
         -6.9403294604745806e-004,...
         -6.9718795113443677e-004,...
         -7.0035347451613788e-004,...
         -7.0352939701083387e-004,...
         -7.0671559904783956e-004,...
         -7.0991196066823065e-004,...
         -7.1311836153086698e-004,...
         -7.1633468091565188e-004,...
         -7.1956079773151924e-004,...
         -7.2279659051566138e-004,...
         -7.2604193744291667e-004,...
         -7.2929671632825652e-004,...
         -7.3256080463095382e-004,...
         -7.3583407945772056e-004,...
         -7.3911641757537538e-004,...
         -7.4240769540201770e-004,...
         -7.4570778902800533e-004,...
         -7.4901657419952075e-004,...
         -7.5233392634805663e-004,...
         -7.5565972057633720e-004,...
         -7.5899383166889252e-004,...
         -7.6233613409854386e-004,...
         -7.6568650203067789e-004,...
         -7.6904480932371815e-004,...
         -7.7241092954110756e-004,...
         -7.7578473594992910e-004,...
         -7.7916610152794813e-004,...
         -7.8255489896851234e-004,...
         -7.8595100068665279e-004,...
         -7.8935427881863293e-004,...
         -7.9276460523487768e-004,...
         -7.9618185153881036e-004,...
         -7.9960588907068068e-004,...
         -8.0303658892049139e-004,...
         -8.0647382192052300e-004,...
         -8.0991745866543837e-004,...
         -8.1336736950144740e-004,...
         -8.1682342454097491e-004,...
         -8.2028549366745004e-004,...
         -8.2375344653398859e-004,...
         -8.2722715257538932e-004,...
         -8.3070648100818441e-004,...
         -8.3419130083789493e-004,...
         -8.3768148086229535e-004,...
         -8.4117688967936686e-004,...
         -8.4467739568725862e-004,...
         -8.4818286709532138e-004,...
         -8.5169317192394193e-004,...
         -8.5520817801219623e-004,...
         -8.5872775302191673e-004,...
         -8.6225176444585695e-004,...
         -8.6578007959912582e-004,...
         -8.6931256565128147e-004,...
         -8.7284908960165821e-004,...
         -8.7638951830460094e-004,...
         -8.7993371846263363e-004,...
         -8.8348155664009706e-004,...
         -8.8703289926243508e-004,...
         -8.9058761262395111e-004,...
         -8.9414556289069982e-004,...
         -8.9770661610938363e-004,...
         -9.0127063820671081e-004,...
         -9.0483749500043487e-004,...
         -9.0840705219989834e-004,...
         -9.1197917541398536e-004,...
         -9.1555373015291973e-004,...
         -9.1913058183862675e-004,...
         -9.2270959580295923e-004,...
         -9.2629063729713365e-004,...
         -9.2987357150189996e-004,...
         -9.3345826351563647e-004,...
         -9.3704457838072802e-004,...
         -9.4063238107318381e-004,...
         -9.4422153651356922e-004,...
         -9.4781190957412957e-004,...
         -9.5140336507721778e-004,...
         -9.5499576780775253e-004,...
         -9.5858898251352845e-004,...
         -9.6218287391255561e-004,...
         -9.6577730669536390e-004,...
         -9.6937214553583656e-004,...
         -9.7296725508833905e-004,...
         -9.7656249999973632e-004,...
         -9.8015774491159524e-004,...
         -9.8375285446517836e-004,...
         -9.8734769330334243e-004,...
         -9.9094212608782429e-004,...
         -9.9453601748523903e-004,...
         -9.9812923219385415e-004,...
         -1.0017216349224919e-003,...
         -1.0053130904257195e-003,...
         -1.0089034634862156e-003,...
         -1.0124926189272569e-003,...
         -1.0160804216190922e-003,...
         -1.0196667364847556e-003,...
         -1.0232514284986278e-003,...
         -1.0268343627016230e-003,...
         -1.0304154041975936e-003,...
         -1.0339944181621320e-003,...
         -1.0375712698467256e-003,...
         -1.0411458245869116e-003,...
         -1.0447179477994966e-003,...
         -1.0482875049988524e-003,...
         -1.0518543617935349e-003,...
         -1.0554183838904399e-003,...
         -1.0589794371099869e-003,...
         -1.0625373873757048e-003,...
         -1.0660921007374655e-003,...
         -1.0696434433600161e-003,...
         -1.0731912815365435e-003,...
         -1.0767354816956800e-003,...
         -1.0802759103976426e-003,...
         -1.0838124343488292e-003,...
         -1.0873449204001388e-003,...
         -1.0908732355563375e-003,...
         -1.0943972469772858e-003,...
         -1.0979168219879746e-003,...
         -1.1014318280760081e-003,...
         -1.1049421329050329e-003,...
         -1.1084476043126288e-003,...
         -1.1119481103204422e-003,...
         -1.1154435191360510e-003,...
         -1.1189336991638207e-003,...
         -1.1224185189900567e-003,...
         -1.1258978474244317e-003,...
         -1.1293715534659461e-003,...
         -1.1328395063340180e-003,...
         -1.1363015754592711e-003,...
         -1.1397576304990355e-003,...
         -1.1432075413341247e-003,...
         -1.1466511780786042e-003,...
         -1.1500884110797621e-003,...
         -1.1535191109296183e-003,...
         -1.1569431484618753e-003,...
         -1.1603603947651097e-003,...
         -1.1637707211809407e-003,...
         -1.1671739993140997e-003,...
         -1.1705701010308906e-003,...
         -1.1739588984724871e-003,...
         -1.1773402640506522e-003,...
         -1.1807140704573960e-003,...
         -1.1840801906769292e-003,...
         -1.1874384979694140e-003,...
         -1.1907888659011961e-003,...
         -1.1941311683316233e-003,...
         -1.1974652794231658e-003,...
         -1.2007910736516827e-003,...
         -1.2041084257999564e-003,...
         -1.2074172109732253e-003,...
         -1.2107173045970243e-003,...
         -1.2140085824255307e-003,...
         -1.2172909205413818e-003,...
         -1.2205641953699415e-003,...
         -1.2238282836715085e-003,...
         -1.2270830625561984e-003,...
         -1.2303284094840141e-003,...
         -1.2335642022700769e-003,...
         -1.2367903190831141e-003,...
         -1.2400066384698333e-003,...
         -1.2432130393304434e-003,...
         -1.2464094009542839e-003,...
         -1.2495956029886976e-003,...
         -1.2527715254834480e-003,...
         -1.2559370488653449e-003,...
         -1.2590920539529049e-003,...
         -1.2622364219615281e-003,...
         -1.2653700345104121e-003,...
         -1.2684927736165063e-003,...
         -1.2716045217140651e-003,...
         -1.2747051616462880e-003,...
         -1.2777945766759185e-003,...
         -1.2808726504871461e-003,...
         -1.2839392671953808e-003,...
         -1.2869943113393892e-003,...
         -1.2900376679022599e-003,...
         -1.2930692223035193e-003,...
         -1.2960888604027364e-003,...
         -1.2990964685181941e-003,...
         -1.3020919334088329e-003,...
         -1.3050751423026425e-003,...
         -1.3080459828813712e-003,...
         -1.3110043432925497e-003,...
         -1.3139501121594164e-003,...
         -1.3168831785717470e-003,...
         -1.3198034321038369e-003,...
         -1.3227107628081163e-003,...
         -1.3256050612268254e-003,...
         -1.3284862183889751e-003,...
         -1.3313541258235550e-003,...
         -1.3342086755521784e-003,...
         -1.3370497601056619e-003,...
         -1.3398772725167229e-003,...
         -1.3426911063327707e-003,...
         -1.3454911556128766e-003,...
         -1.3482773149433404e-003,...
         -1.3510494794107422e-003,...
         -1.3538075446614803e-003,...
         -1.3565514068476937e-003,...
         -1.3592809626683643e-003,...
         -1.3619961093534303e-003,...
         -1.3646967446821229e-003,...
         -1.3673827669748412e-003,...
         -1.3700540751065695e-003,...
         -1.3727105685012108e-003,...
         -1.3753521471465584e-003,...
         -1.3779787115855160e-003,...
         -1.3805901629320725e-003,...
         -1.3831864028643771e-003,...
         -1.3857673336384166e-003,...
         -1.3883328580797424e-003,...
         -1.3908828796016820e-003,...
         -1.3934173021952581e-003,...
         -1.3959360304379980e-003,...
         -1.3984389695099390e-003,...
         -1.4009260251664952e-003,...
         -1.4033971037781005e-003,...
         -1.4058521123089618e-003,...
         -1.4082909583258061e-003,...
         -1.4107135500131848e-003,...
         -1.4131197961569702e-003,...
         -1.4155096061658861e-003,...
         -1.4178828900640824e-003,...
         -1.4202395585006765e-003,...
         -1.4225795227437799e-003,...
         -1.4249026947011452e-003,...
         -1.4272089869017883e-003,...
         -1.4294983125168104e-003,...
         -1.4317705853552957e-003,...
         -1.4340257198691398e-003,...
         -1.4362636311438218e-003,...
         -1.4384842349378915e-003,...
         -1.4406874476344963e-003,...
         -1.4428731862975550e-003,...
         -1.4450413686171440e-003,...
         -1.4471919129757605e-003,...
         -1.4493247384044024e-003,...
         -1.4514397646015829e-003,...
         -1.4535369119363767e-003,...
         -1.4556161014574929e-003,...
         -1.4576772548770896e-003,...
         -1.4597202946002980e-003,...
         -1.4617451437055880e-003,...
         -1.4637517259590640e-003,...
         -1.4657399658114950e-003,...
         -1.4677097884125247e-003,...
         -1.4696611195910063e-003,...
         -1.4715938858872323e-003,...
         -1.4735080145342307e-003,...
         -1.4754034334586432e-003,...
         -1.4772800713098080e-003,...
         -1.4791378574218648e-003,...
         -1.4809767218579694e-003,...
         -1.4827965953846645e-003,...
         -1.4845974094806315e-003,...
         -1.4863790963534065e-003,...
         -1.4881415889167462e-003,...
         -1.4898848208181634e-003,...
         -1.4916087264221886e-003,...
         -1.4933132408289761e-003,...
         -1.4949982998602820e-003,...
         -1.4966638400806013e-003,...
         -1.4983097987768005e-003,...
         -1.4999361139840155e-003,...
         -1.5015427244669563e-003,...
         -1.5031295697420038e-003,...
         -1.5046965900619739e-003,...
         -1.5062437264447403e-003,...
         -1.5077709206096951e-003,...
         -1.5092781150915857e-003,...
         -1.5107652531282604e-003,...
         -1.5122322787354085e-003,...
         -1.5136791366732690e-003,...
         -1.5151057724752683e-003,...
         -1.5165121324251267e-003,...
         -1.5178981635806494e-003,...
         -1.5192638137517241e-003,...
         -1.5206090315284637e-003,...
         -1.5219337662571789e-003,...
         -1.5232379680680040e-003,...
         -1.5245215878532715e-003,...
         -1.5257845772935100e-003,...
         -1.5270268888284955e-003,...
         -1.5282484756937668e-003,...
         -1.5294492918942310e-003,...
         -1.5306292922116836e-003,...
         -1.5317884322393392e-003,...
         -1.5329266683147052e-003,...
         -1.5340439575981038e-003,...
         -1.5351402580225835e-003,...
         -1.5362155283022733e-003,...
         -1.5372697279690765e-003,...
         -1.5383028173211741e-003,...
         -1.5393147574695250e-003,...
         -1.5403055103129742e-003,...
         -1.5412750385552525e-003,...
         -1.5422233056805405e-003,...
         -1.5431502760040860e-003,...
         -1.5440559146133672e-003,...
         -1.5449401874162664e-003,...
         -1.5458030611235122e-003,...
         -1.5466445032538019e-003,...
         -1.5474644820950159e-003,...
         -1.5482629668209223e-003,...
         -1.5490399273316207e-003,...
         -1.5497953344156923e-003,...
         -1.5505291595796691e-003,...
         -1.5512413752336504e-003,...
         -1.5519319545563874e-003,...
         -1.5526008715434358e-003,...
         -1.5532481010048266e-003,...
         -1.5538736185917634e-003,...
         -1.5544774007272702e-003,...
         -1.5550594247012268e-003,...
         -1.5556196685917950e-003,...
         -1.5561581113094467e-003,...
         -1.5566747325711453e-003,...
         -1.5571695129517542e-003,...
         -1.5576424337880109e-003,...
         -1.5580934773008616e-003,...
         -1.5585226265121077e-003,...
         -1.5589298652269276e-003,...
         -1.5593151781716126e-003,...
         -1.5596785507825987e-003,...
         -1.5600199694113057e-003,...
         -1.5603394212066399e-003,...
         -1.5606368941099365e-003,...
         -1.5609123769646232e-003,...
         -1.5611658593586797e-003,...
         -1.5613973317814560e-003,...
         -1.5616067854865179e-003,...
         -1.5617942126273136e-003,...
         -1.5619596060966346e-003,...
         -1.5621029597283848e-003,...
         -1.5622242680576773e-003,...
         -1.5623235265918175e-003,...
         -1.5624007315040481e-003,...
         -1.5624558800098583e-003,...
         -1.5624889698402458e-003,...
          9.9843749999999998e-001,...
         -1.5624889699440536e-003,...
         -1.5624558799578890e-003,...
         -1.5624007315379333e-003,...
         -1.5623235265677950e-003,...
         -1.5622242680764190e-003,...
         -1.5621029597110881e-003,...
         -1.5619596061134974e-003,...
         -1.5617942126131350e-003,...
         -1.5616067854978095e-003,...
         -1.5613973317734043e-003,...
         -1.5611658593649809e-003,...
         -1.5609123769557201e-003,...
         -1.5606368941219002e-003,...
         -1.5603394211950547e-003,...
         -1.5600199694179851e-003,...
         -1.5596785507863938e-003,...
         -1.5593151781575938e-003,...
         -1.5589298652445877e-003,...
         -1.5585226264974179e-003,...
         -1.5580934773070016e-003,...
         -1.5576424337929663e-003,...
         -1.5571695129401740e-003,...
         -1.5566747325799833e-003,...
         -1.5561581113055262e-003,...
         -1.5556196685926787e-003,...
         -1.5550594247025157e-003,...
         -1.5544774007312464e-003,...
         -1.5538736185827784e-003,...
         -1.5532481010111022e-003,...
         -1.5526008715400236e-003,...
         -1.5519319545563726e-003,...
         -1.5512413752388791e-003,...
         -1.5505291595829568e-003,...
         -1.5497953343963735e-003,...
         -1.5490399273454351e-003,...
         -1.5482629668179771e-003,...
         -1.5474644821068318e-003,...
         -1.5466445032370868e-003,...
         -1.5458030611260002e-003,...
         -1.5449401874211000e-003,...
         -1.5440559146144672e-003,...
         -1.5431502760015980e-003,...
         -1.5422233056839392e-003,...
         -1.5412750385487789e-003,...
         -1.5403055103148052e-003,...
         -1.5393147574705871e-003,...
         -1.5383028173224247e-003,...
         -1.5372697279670969e-003,...
         -1.5362155283060515e-003,...
         -1.5351402580174893e-003,...
         -1.5340439575999480e-003,...
         -1.5329266683177692e-003,...
         -1.5317884322345774e-003,...
         -1.5306292922186179e-003,...
         -1.5294492918893157e-003,...
         -1.5282484756925850e-003,...
         -1.5270268888303207e-003,...
         -1.5257845772915308e-003,...
         -1.5245215878564331e-003,...
         -1.5232379680664440e-003,...
         -1.5219337662572528e-003,...
         -1.5206090315260845e-003,...
         -1.5192638137541100e-003,...
         -1.5178981635809797e-003,...
         -1.5165121324288292e-003,...
         -1.5151057724740113e-003,...
         -1.5136791366717026e-003,...
         -1.5122322787296570e-003,...
         -1.5107652531281034e-003,...
         -1.5092781150926199e-003,...
         -1.5077709206187222e-003,...
         -1.5062437264342986e-003,...
         -1.5046965900700855e-003,...
         -1.5031295697430958e-003,...
         -1.5015427244660729e-003,...
         -1.4999361139794820e-003,...
         -1.4983097987756513e-003,...
         -1.4966638400789917e-003,...
         -1.4949982998634724e-003,...
         -1.4933132408292559e-003,...
         -1.4916087264238633e-003,...
         -1.4898848208170807e-003,...
         -1.4881415889171868e-003,...
         -1.4863790963521124e-003,...
         -1.4845974094830749e-003,...
         -1.4827965953828183e-003,...
         -1.4809767218581622e-003,...
         -1.4791378574222063e-003,...
         -1.4772800713058960e-003,...
         -1.4754034334615927e-003,...
         -1.4735080145321332e-003,...
         -1.4715938858897713e-003,...
         -1.4696611195928616e-003,...
         -1.4677097884095553e-003,...
         -1.4657399658131701e-003,...
         -1.4637517259579751e-003,...
         -1.4617451437058458e-003,...
         -1.4597202946008952e-003,...
         -1.4576772548781142e-003,...
         -1.4556161014545096e-003,...
         -1.4535369119377797e-003,...
         -1.4514397646006769e-003,...
         -1.4493247384047333e-003,...
         -1.4471919129781137e-003,...
         -1.4450413686186827e-003,...
         -1.4428731862909726e-003,...
         -1.4406874476391913e-003,...
         -1.4384842349370332e-003,...
         -1.4362636311478700e-003,...
         -1.4340257198637812e-003,...
         -1.4317705853555259e-003,...
         -1.4294983125176740e-003,...
         -1.4272089869016269e-003,...
         -1.4249026947001754e-003,...
         -1.4225795227455896e-003,...
         -1.4202395584991126e-003,...
         -1.4178828900651519e-003,...
         -1.4155096061662933e-003,...
         -1.4131197961572452e-003,...
         -1.4107135500121479e-003,...
         -1.4082909583273593e-003,...
         -1.4058521123071414e-003,...
         -1.4033971037784184e-003,...
         -1.4009260251672689e-003,...
         -1.3984389695073079e-003,...
         -1.3959360304414813e-003,...
         -1.3934173021936680e-003,...
         -1.3908828796013201e-003,...
         -1.3883328580810712e-003,...
         -1.3857673336377231e-003,...
         -1.3831864028656842e-003,...
         -1.3805901629316772e-003,...
         -1.3779787115856205e-003,...
         -1.3753521471449885e-003,...
         -1.3727105685011161e-003,...
         -1.3700540751047667e-003,...
         -1.3673827669747377e-003,...
         -1.3646967446813579e-003,...
         -1.3619961093540838e-003,...
         -1.3592809626680889e-003,...
         -1.3565514068500930e-003,...
         -1.3538075446624864e-003,...
         -1.3510494794139935e-003,...
         -1.3482773149359379e-003,...
         -1.3454911556155522e-003,...
         -1.3426911063340865e-003,...
         -1.3398772725183798e-003,...
         -1.3370497601060175e-003,...
         -1.3342086755528714e-003,...
         -1.3313541258221440e-003,...
         -1.3284862183889951e-003,...
         -1.3256050612252186e-003,...
         -1.3227107628077522e-003,...
         -1.3198034321029242e-003,...
         -1.3168831785717282e-003,...
         -1.3139501121583739e-003,...
         -1.3110043432937395e-003,...
         -1.3080459828806736e-003,...
         -1.3050751423036074e-003,...
         -1.3020919334109907e-003,...
         -1.2990964685168907e-003,...
         -1.2960888604043462e-003,...
         -1.2930692223018258e-003,...
         -1.2900376679029302e-003,...
         -1.2869943113398635e-003,...
         -1.2839392671934497e-003,...
         -1.2808726504881507e-003,...
         -1.2777945766755130e-003,...
         -1.2747051616464173e-003,...
         -1.2716045217145064e-003,...
         -1.2684927736172260e-003,...
         -1.2653700345088591e-003,...
         -1.2622364219624167e-003,...
         -1.2590920539523218e-003,...
         -1.2559370488651094e-003,...
         -1.2527715254845309e-003,...
         -1.2495956029893842e-003,...
         -1.2464094009499052e-003,...
         -1.2432130393332400e-003,...
         -1.2400066384691630e-003,...
         -1.2367903190853551e-003,...
         -1.2335642022672615e-003,...
         -1.2303284094845233e-003,...
         -1.2270830625574190e-003,...
         -1.2238282836720079e-003,...
         -1.2205641953694803e-003,...
         -1.2172909205420946e-003,...
         -1.2140085824240685e-003,...
         -1.2107173045970956e-003,...
         -1.2074172109733175e-003,...
         -1.2041084258002299e-003,...
         -1.2007910736511554e-003,...
         -1.1974652794242019e-003,...
         -1.1941311683306866e-003,...
         -1.1907888659012736e-003,...
         -1.1874384979700260e-003,...
         -1.1840801906752058e-003,...
         -1.1807140704599229e-003,...
         -1.1773402640493408e-003,...
         -1.1739588984715200e-003,...
         -1.1705701010314650e-003,...
         -1.1671739993135541e-003,...
         -1.1637707211815777e-003,...
         -1.1603603947648480e-003,...
         -1.1569431484617994e-003,...
         -1.1535191109288505e-003,...
         -1.1500884110807342e-003,...
         -1.1466511780789752e-003,...
         -1.1432075413353340e-003,...
         -1.1397576304990747e-003,...
         -1.1363015754586390e-003,...
         -1.1328395063318860e-003,...
         -1.1293715534660170e-003,...
         -1.1258978474246091e-003,...
         -1.1224185189932100e-003,...
         -1.1189336991603155e-003,...
         -1.1154435191385980e-003,...
         -1.1119481103209646e-003,...
         -1.1084476043121299e-003,...
         -1.1049421329037969e-003,...
         -1.1014318280756653e-003,...
         -1.0979168219872753e-003,...
         -1.0943972469782874e-003,...
         -1.0908732355563223e-003,...
         -1.0873449204004161e-003,...
         -1.0838124343485979e-003,...
         -1.0802759103978442e-003,...
         -1.0767354816952960e-003,...
         -1.0731912815376409e-003,...
         -1.0696434433593548e-003,...
         -1.0660921007372762e-003,...
         -1.0625373873766634e-003,...
         -1.0589794371081978e-003,...
         -1.0554183838913415e-003,...
         -1.0518543617929401e-003,...
         -1.0482875050002235e-003,...
         -1.0447179478003921e-003,...
         -1.0411458245856058e-003,...
         -1.0375712698470865e-003,...
         -1.0339944181616040e-003,...
         -1.0304154041974837e-003,...
         -1.0268343627020163e-003,...
         -1.0232514284991443e-003,...
         -1.0196667364836065e-003,...
         -1.0160804216195738e-003,...
         -1.0124926189268922e-003,...
         -1.0089034634860796e-003,...
         -1.0053130904267387e-003,...
         -1.0017216349231439e-003,...
         -9.9812923219055254e-004,...
         -9.9453601748738271e-004,...
         -9.9094212608747301e-004,...
         -9.8734769330502555e-004,...
         -9.8375285446340071e-004,...
         -9.8015774491163644e-004,...
         -9.7656250000026368e-004,...
         -9.7296725508843425e-004,...
         -9.6937214553558058e-004,...
         -9.6577730669627159e-004,...
         -9.6218287391215262e-004,...
         -9.5858898251385003e-004,...
         -9.5499576780783797e-004,...
         -9.5140336507714795e-004,...
         -9.4781190957342093e-004,...
         -9.4422153651413929e-004,...
         -9.4063238107267109e-004,...
         -9.3704457838060290e-004,...
         -9.3345826351600336e-004,...
         -9.2987357150032819e-004,...
         -9.2629063729922443e-004,...
         -9.2270959580196242e-004,...
         -9.1913058183768989e-004,...
         -9.1555373015326613e-004,...
         -9.1197917541349432e-004,...
         -9.0840705220021178e-004,...
         -9.0483749500049277e-004,...
         -9.0127063820680958e-004,...
         -8.9770661610883156e-004,...
         -8.9414556289111550e-004,...
         -8.9058761262366900e-004,...
         -8.8703289926282507e-004,...
         -8.8348155664053800e-004,...
         -8.7993371846317996e-004,...
         -8.7638951830442031e-004,...
         -8.7284908960285604e-004,...
         -8.6931256565128787e-004,...
         -8.6578007960032223e-004,...
         -8.6225176444147851e-004,...
         -8.5872775302250806e-004,...
         -8.5520817801254675e-004,...
         -8.5169317192438742e-004,...
         -8.4818286709583530e-004,...
         -8.4467739568798309e-004,...
         -8.4117688967922202e-004,...
         -8.3768148086305603e-004,...
         -8.3419130083796475e-004,...
         -8.3070648100854784e-004,...
         -8.2722715257557006e-004,...
         -8.2375344653404497e-004,...
         -8.2028549366665597e-004,...
         -8.1682342454111585e-004,...
         -8.1336736950044452e-004,...
         -8.0991745866510183e-004,...
         -8.0647382192190222e-004,...
         -8.0303658891901363e-004,...
         -7.9960588907085144e-004,...
         -7.9618185153751701e-004,...
         -7.9276460523516738e-004,...
         -7.8935427881884836e-004,...
         -7.8595100068570400e-004,...
         -7.8255489896913197e-004,...
         -7.7916610152804365e-004,...
         -7.7578473595008067e-004,...
         -7.7241092954158993e-004,...
         -7.6904480932414631e-004,...
         -7.6568650202988491e-004,...
         -7.6233613409898643e-004,...
         -7.5899383166879689e-004,...
         -7.5565972057629730e-004,...
         -7.5233392634911861e-004,...
         -7.4901657420029248e-004,...
         -7.4570778902545930e-004,...
         -7.4240769540386226e-004,...
         -7.3911641757502485e-004,...
         -7.3583407945879815e-004,...
         -7.3256080462962762e-004,...
         -7.2929671632822888e-004,...
         -7.2604193744346701e-004,...
         -7.2279659051580211e-004,...
         -7.1956079773114487e-004,...
         -7.1633468091587956e-004,...
         -7.1311836153014501e-004,...
         -7.0991196066808645e-004,...
         -7.0671559904797432e-004,...
         -7.0352939701107944e-004,...
         -7.0035347451591562e-004,...
         -6.9718795113510540e-004,...
         -6.9403294604732406e-004,...
         -6.9088857803788857e-004,...
         -6.8775496549051637e-004,...
         -6.8463222638246834e-004,...
         -6.8152047828675029e-004,...
         -6.7841983835330018e-004,...
         -6.7533042332391068e-004,...
         -6.7225234951258860e-004,...
         -6.6918573280551352e-004,...
         -6.6613068866049454e-004,...
         -6.6308733209743682e-004,...
         -6.6005577769721099e-004,...
         -6.5703613959613831e-004,...
         -6.5402853148267587e-004,...
         -6.5103306659013149e-004,...
         -6.4804985769709796e-004,...
         -6.4507901711906897e-004,...
         -6.4212065670654151e-004,...
         -6.3917488784035587e-004,...
         -6.3624182142834056e-004,...
         -6.3332156789663754e-004,...
         -6.3041423719312748e-004,...
         -6.2751993877295010e-004,...
         -6.2463878161174902e-004,...
         -6.2177087417739562e-004,...
         -6.1891632444724854e-004,...
         -6.1607523989392645e-004,...
         -6.1324772748236832e-004,...
         -6.1043389366639703e-004,...
         -6.0763384438608623e-004,...
         -6.0484768506039138e-004,...
         -6.0207552058766096e-004,...
         -5.9931745533808726e-004,...
         -5.9657359315122408e-004,...
         -5.9384403733174149e-004,...
         -5.9112889064659674e-004,...
         -5.8842825531798160e-004,...
         -5.8574223302488833e-004,...
         -5.8307092489483865e-004,...
         -5.8041443149798666e-004,...
         -5.7777285285434796e-004,...
         -5.7514628841422329e-004,...
         -5.7253483706867911e-004,...
         -5.6993859713528596e-004,...
         -5.6735766636152618e-004,...
         -5.6479214191959494e-004,...
         -5.6224212039829020e-004,...
         -5.5970769780541165e-004,...
         -5.5718896956045169e-004,...
         -5.5468603049153810e-004,...
         -5.5219897483278963e-004,...
         -5.4972789622182317e-004,...
         -5.4727288769185743e-004,...
         -5.4483404167329492e-004,...
         -5.4241144998774056e-004,...
         -5.4000520384317084e-004,...
         -5.3761539383272356e-004,...
         -5.3524210993615090e-004,...
         -5.3288544149996796e-004,...
         -5.3054547725574545e-004,...
         -5.2822230529884595e-004,...
         -5.2591601309809853e-004,...
         -5.2362668748281104e-004,...
         -5.2135441464448197e-004,...
         -5.1909928013337946e-004,...
         -5.1686136885436889e-004,...
         -5.1464076506250837e-004,...
         -5.1243755236320728e-004,...
         -5.1025181370582503e-004,...
         -5.0808363138208626e-004,...
         -5.0593308702285645e-004,...
         -5.0380026159565269e-004,...
         -5.0168523539897828e-004,...
         -4.9958808806281597e-004,...
         -4.9750889854422050e-004,...
         -4.9544774512181580e-004,...
         -4.9340470540046885e-004,...
         -4.9137985629405054e-004,...
         -4.8937327404119098e-004,...
         -4.8738503418794535e-004,...
         -4.8541521158901606e-004,...
         -4.8346388040823650e-004,...
         -4.8153111411174826e-004,...
         -4.7961698546683019e-004,...
         -4.7772156653962583e-004,...
         -4.7584492869215574e-004,...
         -4.7398714257773644e-004,...
         -4.7214827814179229e-004,...
         -4.7032840461636775e-004,...
         -4.6852759051813140e-004,...
         -4.6674590364705363e-004,...
         -4.6498341108351529e-004,...
         -4.6324017918240954e-004,...
         -4.6151627357758141e-004,...
         -4.5981175916956875e-004,...
         -4.5812670013839994e-004,...
         -4.5646115992047684e-004,...
         -4.5481520122383665e-004,...
         -4.5318888601841704e-004,...
         -4.5158227553350515e-004,...
         -4.4999543025713213e-004,...
         -4.4842840993388020e-004,...
         -4.4688127356024780e-004,...
         -4.4535407938558395e-004,...
         -4.4384688490786109e-004,...
         -4.4235974687170351e-004,...
         -4.4089272126721275e-004,...
         -4.3944586332765817e-004,...
         -4.3801922752524528e-004,...
         -4.3661286757310793e-004,...
         -4.3522683642041354e-004,...
         -4.3386118624673463e-004,...
         -4.3251596847291977e-004,...
         -4.3119123374238432e-004,...
         -4.2988703193294370e-004,...
         -4.2860341214514728e-004,...
         -4.2734042270709286e-004,...
         -4.2609811117065978e-004,...
         -4.2487652430683604e-004,...
         -4.2367570810814383e-004,...
         -4.2249570778500817e-004,...
         -4.2133656776307898e-004,...
         -4.2019833168344394e-004,...
         -4.1908104240097549e-004,...
         -4.1798474197991321e-004,...
         -4.1690947169568016e-004,...
         -4.1585527203227704e-004,...
         -4.1482218267845160e-004,...
         -4.1381024252883222e-004,...
         -4.1281948968676957e-004,...
         -4.1184996144780282e-004,...
         -4.1090169431807404e-004,...
         -4.0997472399688969e-004,...
         -4.0906908538605768e-004,...
         -4.0818481258163579e-004,...
         -4.0732193887537684e-004,...
         -4.0648049675451347e-004,...
         -4.0566051789915712e-004,...
         -4.0486203318042258e-004,...
         -4.0408507266132964e-004,...
         -4.0332966559404181e-004,...
         -4.0259584041875721e-004,...
         -4.0188362476364665e-004,...
         -4.0119304544383554e-004,...
         -4.0052412845844459e-004,...
         -3.9987689899176457e-004,...
         -3.9925138141286526e-004,...
         -3.9864759927009266e-004,...
         -3.9806557529917827e-004,...
         -3.9750533140749458e-004,...
         -3.9696688869214267e-004,...
         -3.9645026742459854e-004,...
         -3.9595548705403548e-004,...
         -3.9548256620951195e-004,...
         -3.9503152269623277e-004,...
         -3.9460237349511654e-004,...
         -3.9419513476403746e-004,...
         -3.9380982183560126e-004,...
         -3.9344644921567224e-004,...
         -3.9310503058550465e-004,...
         -3.9278557879942125e-004,...
         -3.9248810588380257e-004,...
         -3.9221262303922142e-004];

    bpdata = myfiltfilt(numhpz,1,lpdata);

else
    
    % Use of low pass linear phase filter
    [b,a] = butter(7,33/(fs/2));
    lpdata = myfiltfilt(b,a,x);
    
    %HI Pass. Used to eliminate dc
    %component or low frequency drift.
    [b,a] = butter(7,0.7/(fs/2),'high');
    bpdata = myfiltfilt(b,a,lpdata);

end

